package com.lottery.shadowbd.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.adapter.LotRuleAdapter;
import com.lottery.shadowbd.bean.Rule;
import com.lottery.shadowbd.callback.OnMyItemClickListener;
import com.lottery.shadowbd.support.SpacesItemDecoration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class LotRuleActivity extends BaseActivity implements OnMyItemClickListener {

    private ImageView iv_rule_back;

    private RecyclerView rvRule;

    private LotRuleAdapter mLotRuleAdapter;

    private List<Rule> ruleList;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_lot_rule);

        iv_rule_back = (ImageView) findViewById(R.id.iv_rule_back);

        rvRule = (RecyclerView) findViewById(R.id.rvRule);
    }

    @Override
    protected void initData() {
        mContext = this;

        ruleList = new ArrayList<>();

        mLotRuleAdapter = new LotRuleAdapter(mContext, ruleList);

        rvRule.setLayoutManager(new LinearLayoutManager(mContext));
        rvRule.addItemDecoration(new SpacesItemDecoration(2));

        getRule();
    }

    @Override
    protected void initListener() {
        iv_rule_back.setOnClickListener(this);
        mLotRuleAdapter.setMyItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_rule_back://
                finish();
                break;
            default:
                break;
        }
    }

    private void getRule() {

        TypedArray ta = mContext.getResources().obtainTypedArray(R.array.rule_name);
        if (ta != null) {
            for (int i = 0; i < ta.length(); i++) {
                String name = mContext.getResources().getString(ta.getResourceId(i, 0));
                Rule rule = new Rule();
                rule.setName(name);
                ruleList.add(i, rule);

            }
        }
        TypedArray ruleContent = mContext.getResources().obtainTypedArray(R.array.rule);
        if (ruleContent != null) {
            for (int j = 0; j < ruleContent.length(); j++) {
                InputStream in = mContext.getResources().openRawResource(
                        (ruleContent.getResourceId(j, 0)));
                BufferedReader br;
                try {
                    br = new BufferedReader(new InputStreamReader(in, "utf-8"));

                    StringBuffer sb = new StringBuffer();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line.trim() + "\n");
                    }
                    Rule rule = ruleList.get(j);
                    rule.setContent(sb.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        rvRule.setAdapter(mLotRuleAdapter);
    }

    void show(Rule rule) {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(rule.getName()).setMessage(rule.getContent())
                .setPositiveButton("我知道了", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                }).create();
        dialog.show();
    }

    @Override
    public void onItemClick(View view, int position) {
        show(ruleList.get(position));
    }
}
